package com.hpe.aruba.CPPM;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import com.hpe.aruba.CPPM.objects.Device;
import com.hpe.aruba.CPPM.camera.GraphicOverlay;
import com.google.android.gms.vision.MultiProcessor;
import com.google.android.gms.vision.Tracker;
import com.google.android.gms.vision.text.Text;
import com.google.android.gms.vision.text.TextBlock;

import java.util.ArrayList;
import java.util.List;

class OcrTrackerFactory implements MultiProcessor.Factory<TextBlock> {
    private GraphicOverlay mGraphicOverlay;
    private MultiTrackerActivity mContext;

    OcrTrackerFactory(GraphicOverlay graphicOverlay, MultiTrackerActivity context) {
        mGraphicOverlay = graphicOverlay;
        mContext = context;
    }

    @Override
    public Tracker<TextBlock> create(TextBlock textBlock) {
        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, mContext);
        return new GraphicTracker<>(mGraphicOverlay, graphic, mContext);
    }
}

class OcrGraphic extends TrackedGraphic<TextBlock> {

    private static final int TEXT_COLOR = Color.WHITE;

    private static Paint sRectPaint;
    private static Paint sTextPaint;
    private volatile TextBlock mText;
    private MultiTrackerActivity mContext;

    OcrGraphic(GraphicOverlay overlay, MultiTrackerActivity mContext) {
        super(overlay);
        this.mContext = mContext;

        if (sRectPaint == null) {
            sRectPaint = new Paint();
            sRectPaint.setColor(TEXT_COLOR);
            sRectPaint.setStyle(Paint.Style.STROKE);
            sRectPaint.setStrokeWidth(4.0f);
        }

        if (sTextPaint == null) {
            sTextPaint = new Paint();
            sTextPaint.setColor(TEXT_COLOR);
            sTextPaint.setTextSize(54.0f);
        }
        // Redraw the overlay, as this graphic has been added.
        postInvalidate();
    }

    /**
     * Draws the text block annotations for position, size, and raw value on the supplied canvas.
     */
    @Override
    public void draw(Canvas canvas) {
        TextBlock textBlock = mText;
        if (textBlock == null) {
            return;
        }

        List<? extends Text> components = textBlock.getComponents();
        List<Text> elements = new ArrayList<>();
        checkText(components, elements);
        List<Device> currentElements = new ArrayList<>();

        for (Text e : elements) {
            String text = e.getValue();
            text = getMacAdressMatcher().trimMac(text);
            if (!getMacAdressMatcher().isMac(text, false)) {
                continue;
            }
            Device device = new Device(text);
            int paintColor = mContext.isMacAdded(device) ? Color.GREEN : Color.WHITE;
            sRectPaint.setColor(paintColor);
            sTextPaint.setColor(paintColor);
            // Draws the bounding box around the TextBlock.
            RectF rect = new RectF(e.getBoundingBox());
            rect.left = translateX(rect.left);
            rect.top = translateY(rect.top);
            rect.right = translateX(rect.right);
            rect.bottom = translateY(rect.bottom);
            canvas.drawRect(rect, sRectPaint);

            // Break the text into multiple lines and draw each one according to its own bounding box.
            float left = translateX(e.getBoundingBox().left);
            float bottom = translateY(e.getBoundingBox().bottom);
            canvas.drawText(text, left, bottom, sTextPaint);
            currentElements.add(device);
        }
        mContext.processMacs(currentElements);
    }


    private void checkText(List<? extends Text> components, List<Text> elements) {
        for (Text t : components) {
            List<? extends Text> children = t.getComponents();
            if (children.size() == 0) {
                elements.add(t);
            } else {
                checkText(children, elements);
            }
        }
    }

    @Override
    void updateItem(TextBlock text) {
        mText = text;
        postInvalidate();
    }

}